package edu.calpoly.android.echoes;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

public class ColorSelectMenu {
	public enum DrawMode { DKGRAY, MAGENTA, BLUE, GREEN, RED, WHITE, LIGHTBLUE, ORANGE };
	public static final float[] COLOR_FREQUENCIES = { (float) .6, (float) .7, (float) .9, (float) 1.1, (float) 1.5, (float) 1.7, (float) 1.0, (float) 1.0 };
	private int BUTTON_WIDTH = 50;
	private int BUTTON_HEIGHT = 50;
	private int BUTTON_PADDING = 5;
	private static int NUM_BUTTONS = 8;
	
	private DrawMode mode;
	private int modeIndex;
	
	public ColorSelectMenu() {
		mode = DrawMode.MAGENTA;
		modeIndex = 1;
	}
	
	public void setModeIndex(int index) {
		modeIndex = index;
	}
	
	public int getModeIndex() {
		return modeIndex;
	}
	
	public int getCurrentColor() {
		return modeToColor(mode.values()[modeIndex]);
	}
	
	public boolean isMenuPress(int x, int y) {
		if(x < (BUTTON_WIDTH*DrawMode.values().length) && y < BUTTON_HEIGHT) {

			return true;
		}
		return false;
	}
	
	public MyRectangle getMenuMyRectangle() {
		return new MyRectangle(0, 0,BUTTON_WIDTH*NUM_BUTTONS,BUTTON_HEIGHT, new Paint(getCurrentColor()));
	}
	
	public void setMenuSelection(int x, int y) {
		mode = DrawMode.values()[x/BUTTON_WIDTH];
		modeIndex = x/BUTTON_WIDTH;
	}
	
	@SuppressWarnings("static-access")
	public void drawMenu(Canvas c) {
		BUTTON_WIDTH = c.getWidth()/NUM_BUTTONS;
		BUTTON_HEIGHT = BUTTON_WIDTH;
		
		Paint p = new Paint();
		p.setColor(Color.BLACK);
		c.drawRect(new RectF(0, 0,
				BUTTON_WIDTH*NUM_BUTTONS, BUTTON_HEIGHT), p);
		
		for(int i = 0 ; i < NUM_BUTTONS ; i++) {			
			if(i == modeIndex) {
				p.setColor(Color.YELLOW);
				c.drawRect(new RectF(i*BUTTON_WIDTH,0, (i+1)*BUTTON_WIDTH, BUTTON_HEIGHT), p);
			}
			p.setColor(modeToColor(mode.values()[i]));
			c.drawRect(new RectF(i*BUTTON_WIDTH + BUTTON_PADDING,
					BUTTON_PADDING, (i+1)*BUTTON_WIDTH - BUTTON_PADDING, BUTTON_HEIGHT - BUTTON_PADDING), p);
		}
	}
	
	
	private int modeToColor(DrawMode selection) {
		switch(selection) {
			case DKGRAY : return Color.DKGRAY;
			case MAGENTA : return Color.MAGENTA;
			case BLUE : return Color.BLUE;
			case GREEN : return Color.GREEN;
			case RED : return Color.RED;
			case WHITE : return Color.WHITE;
			case LIGHTBLUE : return Color.rgb(169,229,255);
			case ORANGE : return Color.rgb(255,130,0);
		}
		return -1;
	}
}
